package com.hd.assistant.dal.dataobject.ordermining;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import com.hd.util.CommonUtil;
import com.taobao.api.domain.Order;
import com.taobao.api.domain.Trade;

public class TcOrder extends com.taobao.api.domain.Trade {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static Integer PROFIT_CAL_SUCCESS = 1;
	public static Integer PROFIT_CAL_NO_COST = -1;
	public static Integer PROFIT_CAL_INIT = 0;
	
	public static String TRADE_NO_CREATE_PAY = "TRADE_NO_CREATE_PAY";
	public static String WAIT_BUYER_PAY = "WAIT_BUYER_PAY";
	public static String SELLER_CONSIGNED_PART = "SELLER_CONSIGNED_PART";
	public static String WAIT_SELLER_SEND_GOODS = "WAIT_SELLER_SEND_GOODS";
	public static String WAIT_BUYER_CONFIRM_GOODS = "WAIT_BUYER_CONFIRM_GOODS";
	public static String TRADE_BUYER_SIGNED = "TRADE_BUYER_SIGNED";
	public static String TRADE_FINISHED = "TRADE_FINISHED";
	public static String TRADE_CLOSED = "TRADE_CLOSED";
	public static String TRADE_CLOSED_BY_TAOBAO = "TRADE_CLOSED_BY_TAOBAO";
	
	private Long id;
	private Long userId;

	private String ordersJson;
	
	private String includeItems;
	
	private String createDate;
	
	private String createMonth;
	
	private String payDate;
	
	private String payMonth;

	private String successDate;
	
	private String successMonth;
	
	private Double sales;
	
	private Double profit;
	
	private Date profitCalTime;
	
	private Integer isProfitCal;
	
	private Double creditCardCost;
	private Double tmallCost;
	
	private Date gmtCreate;
	
	private Date gmtModify;

	private Integer itemNums;
	
	public TcOrder() {
	}

	public TcOrder(Trade t){
		BeanUtils.copyProperties(t, this);//此处暂时用spring beanutil 测试结果看拷贝时间在1ms以内 可以接受
	}
	


	public Double getCreditCardCost() {
		return creditCardCost;
	}

	public void setCreditCardCost(Double creditCardCost) {
		this.creditCardCost = creditCardCost;
	}

	public Double getTmallCost() {
		return tmallCost;
	}

	public void setTmallCost(Double tmallCost) {
		this.tmallCost = tmallCost;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOrdersJson() {
		return ordersJson;
	}

	
	public Integer getItemNums() {
		return itemNums;
	}

	public void setItemNums(Integer itemNums) {
		this.itemNums = itemNums;
	}

	public void setOrdersJson(String ordersJson) {
		this.ordersJson = ordersJson;
		try{
			setOrders(JSONObject.parseArray(ordersJson,Order.class));
		}catch(Exception e){
		}
	}

	public void setOrdersJson(String ordersJson,boolean needparse) {
		if(needparse)
			setOrdersJson(ordersJson);
		else
			this.ordersJson = ordersJson;
		
	}
	
	public String getIncludeItems() {
		return includeItems;
	}

	public void setIncludeItems(String includeItems) {
		this.includeItems = includeItems;
	}

	public String getCreateDate() {
		if(createDate != null)
			return createDate;
		if(getCreated() != null)
			return new SimpleDateFormat("yyyyMMdd").format(getCreated());
		return null;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public String getCreateMonth() {
		if(createMonth != null)
			return createMonth;
		if(getCreated() != null)
			return new SimpleDateFormat("yyyyMM").format(getCreated());
		return null;
	}

	public void setCreateMonth(String createMonth) {
		this.createMonth = createMonth;
	}

	public String getPayDate() {
		if(payDate != null)
			return payDate;
		if(getPayTime()!= null)
			return new SimpleDateFormat("yyyyMMdd").format(getPayTime());
		return null;
	}

	public void setPayDate(String payDate) {
		this.payDate = payDate;
	}

	public String getPayMonth() {
		if(payMonth != null)
			return payMonth;
		if(getPayTime()!= null)
			return new SimpleDateFormat("yyyyMM").format(getPayTime());
		return null;
	}

	public void setPayMonth(String payMonth) {
		this.payMonth = payMonth;
	}

	public String getSuccessDate() {
		if(successDate != null)
			return successDate;
		if(getEndTime()!= null)
			return new SimpleDateFormat("yyyyMMdd").format(getEndTime());
		return null;
	}

	public void setSuccessDate(String successDate) {
		this.successDate = successDate;
	}

	public String getSuccessMonth() {
		if(successMonth != null)
			return successMonth;
		if(getEndTime()!= null)
			return new SimpleDateFormat("yyyyMM").format(getEndTime());
		return null;
	}

	public void setSuccessMonth(String successMonth) {
		this.successMonth = successMonth;
	}

	public Double getSales() {
		return sales;
	}

	public void setSales(Double sales) {
		this.sales = sales;
	}

	public Double getProfit() {
		return profit;
	}

	public void setProfit(Double profit) {
		this.profit = profit;
	}

	public Date getProfitCalTime() {
		return profitCalTime;
	}

	public void setProfitCalTime(Date profitCalTime) {
		this.profitCalTime = profitCalTime;
	}

	public Integer getIsProfitCal() {
		return isProfitCal;
	}


	public void setIsProfitCal(Integer isProfitCal) {
		this.isProfitCal = isProfitCal;
	}
	
	public Date getGmtCreate() {
		return gmtCreate;
	}

	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}

	public Date getGmtModify() {
		return gmtModify;
	}

	public void setGmtModify(Date gmtModify) {
		this.gmtModify = gmtModify;
	}


	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
	
	public String getCreatedDisplay(){
		if(getCreated() != null)
			return CommonUtil.formatDate("yyyy-MM-dd hh:mm:ss", getCreated());
		return null;
	}
	public static void main(String[] args) {
		Trade t = new Trade();
		t.setTid(2l);
		long begin = System.currentTimeMillis();
		//貌似第一次copy比较慢
		for(int i = 0;i<10000;i++){
			t = new Trade();
			t.setTid(Long.valueOf(i));
			new TcOrder(t);
		}
		long end = System.currentTimeMillis();
		System.out.println(end - begin);
		
		String json = "[{'adjustFee':'0.00','buyerRate':false,'cid':50013618,'discountFee':'0.00','isDaixiao':false,'num':1,'numIid':26643836655,'oid':398396919498083,'outerIid':'y0477','payment':'15.00','picPath':'http://img04.taobaocdn.com/bao/uploaded/i4/12663025877903205/T1qnBqFjxeXXXXXXXX_!!0-item_pic.jpg','price':'5.00','refundStatus':'NO_REFUND','sellerRate':false,'sellerType':'C','skuId':'35811059848','skuPropertiesName':'颜色分类:随机发货;参考身高:100cm','status':'WAIT_SELLER_SEND_GOODS','title':'女童三角内裤','totalFee':'5.00'}]";
		List<Order>orders = new ArrayList<Order>();
		orders =(List<Order>) JSONObject.parseArray(json,Order.class);
		Order o = orders.get(0);
		System.out.println(o.getBuyerNick());
	}
	public boolean hasCalProfitSuccess(){
		return this.isProfitCal!=null && this.isProfitCal.equals(PROFIT_CAL_SUCCESS);
	}
	public String getProfitCalStatusDisplay(){
		String display = "未计算利润";
		if(this.isProfitCal == null)
			return null;
		if(this.isProfitCal.equals(TcOrder.PROFIT_CAL_INIT)){
			display = "未计算利润";
		}
		if(this.isProfitCal.equals(TcOrder.PROFIT_CAL_NO_COST)){
			display = "宝贝信息不完整";
		}
		if(this.isProfitCal.equals(TcOrder.PROFIT_CAL_SUCCESS)){
			display = "成功";
		}
		return display;
	}

	public String getStatusDisplay(){
		if(getStatus() == null)
			return null;
		String status = getStatus();
		String display = "无状态";
		if( status.equals(TRADE_NO_CREATE_PAY)){
			display = "没有创建支付宝交易";
		}
		if( status.equals(WAIT_BUYER_PAY)){
			display = "等待买家付款";
		}
		if( status.equals(SELLER_CONSIGNED_PART)){
			display = "卖家部分发货";
		}
		if( status.equals(WAIT_SELLER_SEND_GOODS)){
			display = "买家已付款";
		}
		if( status.equals(WAIT_BUYER_CONFIRM_GOODS)){
			display = "卖家已发货";
		}
		if( status.equals(TRADE_BUYER_SIGNED)){
			display = "买家已签收";
		}
		
		if( status.equals(TRADE_FINISHED)){
			display = "交易成功";
		}
		if( status.equals(TRADE_CLOSED)){
			display = "交易自动关闭";
		}
		if( status.equals(TRADE_CLOSED_BY_TAOBAO)){
			display = "交易关闭";
		}
		
		
		return display;
	}
}
